﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.IO;
using JTicket.Printer;
namespace JTicket.UI.Extention.IHandler
{
    public class TicketSaleHandler : IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { throw new NotImplementedException(); }
        }

        public void ProcessRequest(HttpContext context)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
    public class GetTicketName : IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            string TitleCode = request.QueryString["TitleCode"].ToString();
            string SubCode = request.QueryString["SubCode"].ToString();
            List<Model.TicketName> list = BLL.D_SubAreaProc.GetTicketName(TitleCode, SubCode);
            int flag = list.Count();
            response.Write("[");
            if (flag > 0)
            {
                foreach (var x in list)
                {
                    response.Write(JsonHelper.ObjectToJson(x));
                    if (--flag > 0)
                    {
                        response.Write(",");
                    }
                }
            }
            response.Write("]");
            response.End();
        }

        #endregion
    }
    public class GetSeatList : IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            StringBuilder sb = new StringBuilder();
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            BLL.D_Area bllArea = new BLL.D_Area();
            //横坐标
            int left = 0;
            //纵坐标
            int top = 0;
            int ex = 0;//分区坐标
            string SeatState = string.Empty;//座位状态
            string SeatStr = string.Empty;
            string Cooke = string.Empty;
            string TitleCode = string.Empty;
            string SubCode = request.QueryString["SubCode"].ToString();
            string ThCode = request.QueryString["ThCode"].ToString();
            int Size = 13;
            int Point = 0;
            string IsCow = "0";
            if (request.QueryString["TitleCode"] != null)
            {
                TitleCode = request.QueryString["TitleCode"].ToString();
                Size = int.Parse(request.QueryString["Size"].ToString());
                Point = int.Parse(request.QueryString["Point"].ToString());
                IsCow = request.QueryString["IsCow"].ToString();
            }
            left = Point;
            ex = int.Parse(bllArea.GetModel(SubCode).ex);
            int flag = left + ex * (Size + 1);
            if (context.Request.Cookies["SeatCode"] != null)
            {
                Cooke = context.Request.Cookies["SeatCode"].Value;
            }
            List<Model.D_SubAreaProc> list =
                 BLL.D_SubAreaProc.SeatListGet(TitleCode, SubCode, ThCode);
            BLL.H_BookInfo bllBookInfo = new BLL.H_BookInfo();
            BLL.H_OrderBal bllOrderBal = new BLL.H_OrderBal();
            if (list.Count > 0)
            {
                foreach (var Seat in list)
                {
                    SeatState = Seat.Operation;
                    if (Seat.Visible == "1")
                    {
                        if (left >= flag)
                        {
                            left = Point;
                            top = top + (Size + 1);
                        }
                        sb.Append(string.Format("<div  id=\"G{0}\" name=\"{1}\"  Msg=\"{2}\" onmousedown=\"Choose(this);\"", Seat.Sixid, Seat.Price, Seat.Ticketname));
                        sb.Append("style=\"background:");
                        if (new Common.StringHelp().GetChar(Cooke, string.Format("G{0}%7C", Seat.Sixid)))
                            sb.Append("#992c0a");
                        else
                            sb.Append(Seat.Color);
                        sb.Append(string.Format("; left:{0}px;top: {1}px; height:{2}px;width:{3}px;font-size:1;color:Black; cursor:default;position:absolute;\" ",
                            left, top, Size, Size));
                        switch (SeatState)
                        {
                            case "1":
                                SeatStr = "<strong>状 态：</strong>已 售";
                                break;
                            case "3":

                                Model.H_BookInfo info = bllBookInfo.GetModel(Seat.Ordercode, Seat.ThCode);
                                 SeatStr = "<strong>状 态：</strong>预 留 <hr/><font color=#780795>姓名：" + info.usertxt + "<br>电话：" + info.Usertel + "<br>订单号：" + info.ordercode + "</font>"; 
                                break;
                            case "4":
                                Model.H_OrderBal orderinfo = bllOrderBal.GetModelbySixid(Seat.ThCode, Seat.AreaCode, Seat.TitleCode, Seat.Sixid, Seat.Ordercode);
                                SeatStr = "<strong>状 态：</strong>预 定 <hr/><font color=green>姓名：" + orderinfo.usertxt + "<br>电话：" + orderinfo.usertel + "<br>订单号：" + orderinfo.ordercode + "</font>"; 
                                break;
                            default:
                                SeatStr = "<strong>状 态：</strong>未 售";
                                break;
                        }
                        sb.Append(" onmouseover=\"if(!!isBound){Choose(this);}showTooltip('dHTMLToolTip',event, '");
                        sb.Append(string.Format("<strong>区 域：</strong>{0}<br><strong>类 型：</strong>{1}<br><strong>位 置：</strong>{2}排{3}号<br><strong>价 格：</strong><font color=red>{4}元</font><br><strong>票 类：</strong>{5}<br>",
                            Seat.Note, Seat.Msg, Seat.Row, Seat.Cow,
                            Seat.Price, Seat.Ticketname));
                        sb.Append(SeatStr);
                        sb.Append("', '#F3F2E7','#000000','#000000','20000')\" onMouseOut=\"hideTooltip('dHTMLToolTip')\"");
                        if (IsCow == "1")
                            sb.Append(">" + Seat.Cow + "</div>");
                        else
                            sb.Append("></div>");
                        sb.Append("\n");
                    }
                    else
                        if (left >= flag)
                        {
                            left = Point;
                            top = top + (Size + 1);
                        }
                    left = left + (Size + 1);
                }
            }
            response.Write(sb.ToString());
            response.End();
        }
        #endregion
    }
    public class AddCustom : IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            string name = request.QueryString["Name"].ToString();
            string tel = request.QueryString["Tel"].ToString();
            string address = request.QueryString["Address"].ToString();
            string unit = request.QueryString["Unit"].ToString();
            string remake = request.QueryString["Remake"].ToString();

            JTicket.Model.I_Customer customer = new Model.I_Customer();
            BLL.D_OperInfo bllOper = new BLL.D_OperInfo();
            customer.UserName = name;
            customer.Unit = unit;
            customer.UserAddress = address;
            customer.Remak = remake;
            customer.UserPhone = tel;
            customer.TypeName = "普通客户";
            customer.AddTime = DateTime.Now;
            customer.ThCode = bllOper.Oper().ThCode;
            customer.City = "";
            customer.EMail = "";
            customer.Fav = "";
            customer.Fax = "";
            customer.InCount = 0;
            customer.InPhone = "无记录";
            customer.QQ = "";
            customer.Souce = 0;
            customer.TypeID = "无记录";
            customer.userno = "";
            customer.UserSex = "";
            BLL.I_Customer.Add(customer);

        }
        #endregion
    }
    public class AddCustomInfo : IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            string name = request.QueryString["Name"].ToString(); 
            string work = request.QueryString["work"].ToString();
            string sex=request.QueryString["sex"].ToString();
            string tel = request.QueryString["Tel"].ToString();
            string no=request.QueryString["no"].ToString();
            string fav=request.QueryString["fav"].ToString();
            string unit = request.QueryString["Unit"].ToString();
            string email=request.QueryString["email"].ToString();
            string qq=request.QueryString["qq"].ToString();
            string fax = request.QueryString["fax"].ToString();
            string address = request.QueryString["Address"].ToString();
            string saleinfo = request.QueryString["saleinfo"].ToString();
            string remake = request.QueryString["Remake"].ToString();
            string typename = request.QueryString["typename"].ToString();
            BLL.D_OperInfo bllOper = new BLL.D_OperInfo();
            JTicket.Model.I_Customer customer = new Model.I_Customer();
            customer.UserName = name;
            customer.Unit = unit;
            customer.UserAddress = address;
            customer.Remak = remake;
            customer.UserPhone = tel;
            customer.TypeName = typename;
            customer.AddTime = DateTime.Now;
            customer.ThCode = bllOper.Oper().ThCode;
            customer.City = "";
            customer.EMail = email;
            customer.Fav = fav;
            customer.Fax = fax;
            customer.InCount = 0;
            customer.InPhone = saleinfo;
            customer.QQ = qq;
            customer.Souce = 0;
            customer.TypeID = work;
            customer.userno =no;
            customer.UserSex = sex;
            
            BLL.I_Customer.Add(customer);

        }
        #endregion
    }
    public class SaleTicket : IHttpHandler
    {
        #region IHttpHandler Members
        public bool IsReusable
        {
            get { return false; }
        }
        public void ProcessRequest(HttpContext context)
        {
            StringBuilder sb = new StringBuilder();
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            string sixidList = request.QueryString["Sixid"].ToString();
            string zhekou = request.QueryString["Zhekou"].ToString();
            string salemode =string.Empty;
            if (request.QueryString["SaleModel"].ToString() == "1")
                salemode = "普通销售";
            else
                salemode = "套票销售";
            string username = request.QueryString["UserName"].ToString();
            string usertel = request.QueryString["UserTel"].ToString();
            string useraddress = request.QueryString["UserAddress"].ToString();
            string unitcode = request.QueryString["UnitCode"].ToString();
            string issend = request.QueryString["IsSend"].ToString();
            string paymode = request.QueryString["PayModel"].ToString();
            string taocode = request.QueryString["TaoCode"].ToString();
            string sumamt = request.QueryString["SumAmt"].ToString();
            string sumcount = "0";
            string iccode ="无";
            if (request.QueryString["ICCode"].ToString() != "1")
            {
                iccode = request.QueryString["ICCode"].ToString();
            }
            string titlecode = request.QueryString["TitleCode"].ToString();
            string areacode = request.QueryString["AreaCode"].ToString();
            string msg = request.QueryString["Msg"].ToString();

            BLL.D_OperInfo bllOper = new BLL.D_OperInfo();
            Model.SaleParm parm = new Model.SaleParm();
            Model.D_OperInfo oper = bllOper.Oper();
            
            string thcode = oper.ThCode;
            string Invodio = "无";
            //销售前的系统状态判断。(登录状态,发票剩余状态)
            if (!context.User.Identity.IsAuthenticated)
                response.Write("0");//你的账号还未登录，系统终止售票业务。
            if (BLL.D_Tax.Exists(thcode))
            {
                BLL.D_Tax bllTax = new BLL.D_Tax();
                Model.D_Tax Tax = bllTax.GetModel(thcode);
                if (Tax.fpsy.Equals(Tax.fpsz))
                    response.Write("1");//发票已经用完,系统终止售票业务。";
                else
                    Invodio = Tax.fpsy;
            }
            //数据库销售业务。
            string opercode = oper.OperCode;
            string saleip = HttpContext.Current.Request.UserHostAddress;
            string barcode = string.Empty;
            string Flag = Common.StringHelp.GenerateFileName();
            List<string> aList = new List<string>();
            string[] arr = sixidList.Split('|');
            aList = Common.CheckCode.ListBarCode(arr.Length - 1);
            if (arr.Length-1 > 0)
            {
                for (int i = 0; i < arr.Length - 1; i++)
                {
                    //对象属性赋值
                    barcode = aList[i];
                    parm.AreaCode = areacode;
                    parm.ICCode = iccode;
                    parm.IsSend = int.Parse(issend);
                    parm.Msg = msg;
                    parm.PayModel = paymode;
                    parm.SaleModel = salemode;
                    parm.Sixid = arr[i].ToString().Replace("G", "");
                    parm.SumAmt = decimal.Parse(sumamt);
                    parm.SumCount = int.Parse(sumcount);
                    parm.TaoCode = taocode;
                    parm.ThCode = thcode;
                    parm.TitleCode = titlecode;
                    parm.UnitCode = unitcode;
                    parm.UserAddress = useraddress;
                    parm.UserName = username;
                    parm.UserTel = usertel;
                    parm.Zhekou = zhekou;
                    bool b = BLL.H_TicketBal.Ticket_Sale(parm, opercode, saleip, barcode, Invodio, Flag);
                    if (b)
                    {
                        //生成发票号。
                        if (Invodio != "无")
                        {
                          Print.BuildTaxU(parm.SumAmt.ToString(), parm.UserName, parm.SumCount);
                        }
                        //客户端打印。
                        sb.Append(string.Format("{0}|", Print.MakeSaleCmd
                            (barcode, parm.Sixid, parm.TitleCode, parm.ThCode)));
                    }
                }
                response.Write(sb.ToString().TrimEnd('|'));
                response.End();
            }
            response.Write("2");//销售错误,系统终止售票业务。

        }


        #endregion
    }
    public class RegRprint:IHttpHandler
    {
        #region IHttpHandler 成员
        public bool IsReusable
        {
            get { return false; }
        }
        public void ProcessRequest(HttpContext context)
        {
            BLL.D_OperInfo bllOper = new BLL.D_OperInfo();
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            string Flag = request.QueryString["Flag"].ToString();
            string StitleCode = request.QueryString["StitleCode"].ToString();
            string OperCode = bllOper.Oper().OperCode;
            bool b=false;
            b = DAL.L_RePrintProc.ExcutRePint("", Flag, OperCode, StitleCode, "重打票申请");
            if (b)
            {
                response.Write("已提交审核!");
                response.End();
            }
            response.Write("提交失败,请重新操作!");
        }
        #endregion
    }
    public class Reprint : IHttpHandler
    {
        #region IHttpHandler 成员
        public bool IsReusable
        {
            get { return false; }
        }
        public void ProcessRequest(HttpContext context)
        {
            StringBuilder sb = new StringBuilder();
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            BLL.D_OperInfo bllOper = new BLL.D_OperInfo();
            string BarList = request.QueryString["BarList"].ToString();
            string StitleCode = request.QueryString["StitleCode"].ToString();
            string flag = request.QueryString["flag"].ToString();
            string OperCode = bllOper.Oper().OperCode;
            bool b = false; 
            string[] barArr = BarList.TrimEnd('|').Split('|');
            //判断h_printbal
            if (DAL.H_ShenHe.ShenState(flag) == "0")
            {
                foreach (var x in barArr)
                {
                    
                    b = DAL.L_RePrintProc.ExcutRePint(x, flag, OperCode, StitleCode, "重打票");
                    if (b)
                    {
                        sb.Append(Print.MakeRePrintCmd(x, StitleCode, OperCode));
                        sb.Append("|");
                    }
                    else
                        break;

                }
            }
            else
            {
                response.Write("0");
                response.End();
            }
            if (b)
                response.Write(sb.ToString().TrimEnd('|'));
            else
                response.Write("-1");
        }
        #endregion
    }
}